package com.hdy.oa.action;

import com.hdy.Bean.Dept;
import com.hdy.DBUtils.JDBC;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet({"/dept/list","/dept/add","/dept/details","/dept/modify","/dept/delete"})
public class DeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        String contextPath = request.getContextPath();
        HttpSession session = request.getSession();
        Object username = session.getAttribute("username");
        if (username != null) {
            if ("/dept/list".equals(servletPath)) {
                doList(request,response);
            } else if ("/dept/details".equals(servletPath)) {
                doDetails(request,response);
            } else if ("/dept/add".equals(servletPath)) {
                doAdd(request,response);
            } else if ("/dept/delete".equals(servletPath)) {
                doDel(request,response);
            } else if ("/dept/modify".equals(servletPath)) {
                doModify(request,response);
            }
        }else {
            response.sendRedirect(contextPath);
        }

    }

    private void doModify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String deptNo = request.getParameter("deptNo");
        String dName = request.getParameter("dName");
        String loc = request.getParameter("loc");
        Connection connection=null;
        PreparedStatement ps=null;
        int count=0;

        try {
            connection = JDBC.getConnection();
            String sql="update dept set dname=?,loc=? where deptno=?";
            ps=connection.prepareStatement(sql);
            ps.setString(1,dName);
            ps.setString(2,loc);
            ps.setString(3,deptNo);
            count=ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBC.close(connection,ps,null);
        }
        if (count==1) {
            String contextPath = request.getContextPath();
            response.sendRedirect(contextPath+"/dept/list");
        }
    }


    private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String deptNo = request.getParameter("deptNo");
        Connection connection=null;
        PreparedStatement ps=null;
        int count =0;
        String contextPath = request.getContextPath();

        try {
             connection = JDBC.getConnection();
             connection.setAutoCommit(false);
            String sql = "delete from dept where deptno=?";
            ps=connection.prepareStatement(sql);
            ps.setString(1,deptNo);
            count=ps.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            JDBC.close(connection,ps,null);
        }
        if (count==1) {
            response.sendRedirect(contextPath+"/dept/list");
        }
    }

    private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String deptNo = request.getParameter("deptNo");
        String dName = request.getParameter("dept");
        String loc = request.getParameter("loc");
        Connection connection=null;
        PreparedStatement ps=null;
        int count=0;

        try {
            connection = JDBC.getConnection();
            String sql="insert into dept(DEPTNO,DNAME,LOC) values (?,?,?);";
            ps=connection.prepareStatement(sql);
            ps.setString(1,deptNo);
            ps.setString(2,dName);
            ps.setString(3,loc);
            count=ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBC.close(connection,ps,null);
        }
        if (count==1) {
            String contextPath = request.getContextPath();
            response.sendRedirect(contextPath+"/dept/list");
        }
    }

    private void doDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String deptNo = request.getParameter("deptNo");
        String flag = request.getParameter("flag");
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        Dept dept =new Dept();

        try {
            connection = JDBC.getConnection();
            String sql = "select deptno,dname,loc from dept where deptno=?";
            ps=connection.prepareStatement(sql);
            ps.setString(1,deptNo);
            rs=ps.executeQuery();
            if (rs.next()) {

                dept.setDeptNo(rs.getString("deptno"));
                dept.setDName(rs.getString("dname"));
                dept.setLoc(rs.getString("loc"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBC.close(connection,ps,rs);
        }
        request.setAttribute("dept",dept);
        if (flag.equals("details")) {
            request.getRequestDispatcher("/detail.jsp").forward(request,response);
        }else {
            request.getRequestDispatcher("/modify.jsp").forward(request,response);
        }

    }

    private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        List<Dept> depts=new ArrayList<>();

        try {
            connection= JDBC.getConnection();
            String sql="select DEPTNO,DNAME,LOC from dept";
            ps=connection.prepareStatement(sql);
            rs= ps.executeQuery();
            while (rs.next()){
                String deptNo=rs.getString("DEPTNO");
                String dName=rs.getString("DNAME");
                String loc=rs.getString("LOC");
                Dept dept=new Dept();
                dept.setDeptNo(deptNo);
                dept.setDName(dName);
                dept.setLoc(loc);
                depts.add(dept);
            }
            
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBC.close(connection,ps,rs);
        }

        request.setAttribute("deptList",depts);
        request.getRequestDispatcher("/List.jsp").forward(request,response);
    }
}
